/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DBFunction;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import Constant.Constant;
import Storage.DateManipulation;

public class fAllBuddies {
	public static String process(String currentPhoneNumber)
			throws JSONException {
		JSONArray array = new JSONArray();
		JSONObject object = new JSONObject();
		// get buddies of person form same group
		String sql = "SELECT position.UserId,FirstName,LastName,Latitude,Longitude,LastUpdate,PhoneNumber,Address,Email,IsOnline from buddytracking.user, buddytracking.position where GroupId = ( select GroupId from buddytracking.user where User.PhoneNumber='"
				+ currentPhoneNumber
				+ "') and user.UserId != ( select UserId from buddytracking.user where User.PhoneNumber='"
				+ currentPhoneNumber + "') and position.UserId = user.UserId and GroupId != \"0\"";
		ResultSet resultSet = DB.connectDB.ExecuteQuery(sql);

		try {
			while (resultSet.next()) {
				object.put("firstname", resultSet.getString("FirstName"));
				object.put("lastname", resultSet.getString("LastName"));
				object.put("lat", resultSet.getString("Latitude"));
				object.put("log", resultSet.getString("Longitude"));
				object.put("lastupdate", resultSet.getString("LastUpdate"));
				object.put("phonenumber", resultSet.getString("PhoneNumber"));
				object.put("address", resultSet.getString("Address"));
				object.put("email", resultSet.getString("Email"));
				object.put("IsOnline", resultSet.getString("IsOnline"));
				object.put("lastseen", DateManipulation.timeDifference(
						DateManipulation.StringToDate(resultSet
								.getString("LastUpdate")), DateManipulation
								.StringToDate(Constant.getDateTime())));
				array.put(object.toString());
			}
		} catch (SQLException ex) {
		}
		return array.toString();
	}
}
